package com.yingxin.yxerp.module.activiti.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.activiti.ApprovalStatus;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.dto.AttachmentDto;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.helper.DateHelper;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.commons.utlis.AmtFormat;
import com.yingxin.yx.framework.commons.vo.BusinessTypeRespVo;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.activiti.biz.entity.UpdateDateApply;
import com.yingxin.yxerp.module.activiti.biz.mapper.UpdateDateApplyMapper;
import com.yingxin.yxerp.module.activiti.biz.service.IUpdateDateApplyService;
import com.yingxin.yxerp.module.activiti.biz.service.process.UpdateDateApplyProcessHandler;
import com.yingxin.yxerp.module.activiti.biz.vo.resp.UpdateDateApplyRespVo;
import com.yingxin.yxerp.module.activiti.vo.UpdateDateApplyCreateVo;
import com.yingxin.yxerp.module.attachment.service.IAttachmentRelationService;
import com.yingxin.yxerp.module.common.BusinessTypeHelper;
import com.yingxin.yxerp.module.common.UpdateBusinessType;
import com.yingxin.yxerp.module.sys.service.IActivitiCoreInformationService;
import com.yingxin.yxerp.module.sys.service.IBusinessTypeSettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 编辑申请
 * @author yuebao
 * @since 2023-05-22
 */

@Service
public class UpdateDateApplyServiceImpl
        extends ServiceImpl<UpdateDateApplyMapper, UpdateDateApply>
        implements IUpdateDateApplyService {

    @Autowired
    private IUserSupport userSupport;

    @Autowired
    private IBusinessTypeSettingService businessTypeSettingService;

    @Autowired
    private UpdateDateApplyProcessHandler updateDateApplyProcessHandler;

    @Autowired
    private IActivitiCoreInformationService activitiCoreInformationService;

    @Autowired
    private IAttachmentRelationService attachmentRelationService;

    /**
     * 根据 id 查询
     */
    @Override
    public UpdateDateApplyRespVo getVo(String id ){
        UpdateDateApplyRespVo vo = baseMapper.getVo( id );
        vo.setCreateTimeFormat( DateHelper.dateFormat( vo.getCreateTime(), DateHelper.YYYYMMDDHHMMSS ) );
        vo.setOccurrenceTimeFormat( DateHelper.dateFormat( vo.getOccurrenceTime(), DateHelper.YYYYMMDDHHMMSS ) );
        vo.setBusinessTypeName( BusinessTypeHelper.getName( vo.getBusinessType() ) );
        if( StringUtils.isNotEmpty( vo.getIsPenalty() ) ){
            if( CommonConst.FALSE.equals( vo.getIsPenalty() ) ){
                vo.setIsPenaltyName( "否" );
            }
            if( CommonConst.TRUE.equals( vo.getIsPenalty() ) ){
                vo.setIsPenaltyName( "是" );
            }
        }
        vo.setPenaltyMoneyFormat( AmtFormat.amtTo( vo.getPenaltyMoney() ) );
        return vo;
    }

    /**
     * 获取mapper
     */
    @Override
    public UpdateDateApplyMapper mapper() {
        return baseMapper;
    }

    /**
     * 创建
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create( UpdateDateApplyCreateVo createVo ) {
        // 检查是否正在申请中
        QueryWrapper<UpdateDateApply> qw = new QueryWrapper<>();
        qw.lambda().eq( UpdateDateApply::getBusinessId, createVo.getBusinessId());
        qw.lambda().eq( UpdateDateApply::getApprovalStatus, ApprovalStatus.PENDING_APPROVAL.getStatus());
        List<UpdateDateApply> list = list(qw);
        if (list.size() > 0) {
            throw new BizException("该单据正在编辑申请中，不能重复申请");
        }

        UpdateDateApply apply = new UpdateDateApply();
        BeanUtil.copyProperties( createVo, apply );
        apply.setId( IdHelper.snowflakeId() );
        apply.setApplyNo( businessTypeSettingService.next( BusinessType.XGSQ ) );
        apply.setCreatorId( userSupport.getUserId() );

        save( apply );

        if(CollectionUtil.isNotEmpty( apply.getAttachmentList() ) ){
            // 关联附件信息
            for( AttachmentDto attachment : apply.getAttachmentList() ) {
                attachmentRelationService.create( attachment.getAttachmentId(), apply.getId(), BusinessType.XGSQ.getType(), null );
            }
        }

        // 启动审核流程
        updateDateApplyProcessHandler.startProcessInstance(apply);

        // 塞入app审核列表主要信息
        UpdateDateApplyRespVo vo = getVo( apply.getId() );
        activitiCoreInformationService.create( vo.getId(), BusinessType.XGSQ.getType(), vo );
    }

    /**
     * 修改审核时间
     */
    @Override
    public void updateApprovalTime(String id){
        UpdateWrapper<UpdateDateApply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set( UpdateDateApply::getApprovalTime, new Date() );
        updateWrapper.lambda().eq( UpdateDateApply::getId, id );
        update( updateWrapper );
    }

    /**
     * 获取 业务单据类型 集合
     */
    @Override
    public List<BusinessTypeRespVo> getBusinessTypeList(){
        List<BusinessTypeRespVo> vos = new ArrayList<>();
        for (UpdateBusinessType businessType : UpdateBusinessType.values()) {
            BusinessTypeRespVo businessTypeRespVo = new BusinessTypeRespVo();
            businessTypeRespVo.setBusinessType( businessType.getType() );
            businessTypeRespVo.setBusinessTypeName( businessType.getTypeName() );
            vos.add( businessTypeRespVo );
        }
        return vos;
    }

}
